<template>
  <el-dialog
    v-model="visible"
    title="新增数据表"
    width="600px"
  >
    <el-form :model="form" label-width="100px">
      <el-form-item label="表名称">
        <el-input v-model="form.tableName" />
      </el-form-item>
      <el-form-item label="数据库名">
        <el-input v-model="form.databaseName" />
      </el-form-item>
      <el-form-item label="描述">
        <el-input v-model="form.description" type="textarea" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="handleSubmit">提交</el-button>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import { useTableService } from '@/api/Table/TableApi' // 导入表格服务

// 在组件中使用表格服务
const { createTable, loading: createLoading } = useTableService()

const visible = ref(false)
const form = ref({
  tableName: '',
  databaseName: '',
  description: ''
})

const emit = defineEmits(['success'])
//新增表
const handleSubmit = async () => {
  const response = await createTable({
    tableName: form.value.tableName,
    databaseName: form.value.databaseName,
    description: form.value.description
  })
    // 触发成功事件，传递新增的表格数据
    emit('success', response.data)
    // 关闭对话框
    visible.value = false
    // 重置表单
    form.value = {
      tableName: '',
      databaseName: '',
      description: ''
    }
ElMessage.success("新增成功")
}


// 暴露打开方法
const open = () => {
  visible.value = true
}

defineExpose({ open })
</script>
